package com.example.treehole.ui.page.treehole

import androidx.compose.foundation.lazy.LazyListState
import androidx.lifecycle.ViewModel
import com.example.treehole.service.DataRepository
import com.example.treehole.ui.model.PostInfo
import com.example.treehole.ui.model.PostType
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update

class PostListViewModel : ViewModel() {
    // 最新页面
    private val _latestPostList = MutableStateFlow<List<PostInfo>>(emptyList())
    val latestPostList: StateFlow<List<PostInfo>> = _latestPostList
    private val _latestPage = MutableStateFlow(0)
    val latestPage: StateFlow<Int> = _latestPage
    val latestListState = LazyListState()

    fun updateLatestPostList(posts: List<PostInfo>) {
        _latestPostList.value = posts
    }

    fun addLatestPostList(posts: List<PostInfo>) {
        _latestPostList.update { current ->
            current + posts
        }
    }

    suspend fun refreshLatestPostList(
        jwlToken: String,
        userId: Int,
    ) {
        val res = DataRepository.askNewPost(jwlToken, userId, PostType.NO_TYPE.ordinal, 0)
        res?.let {
            updateLatestPostList(it)
            _latestPage.value = 0
        }
    }

    suspend fun getMoreLatestPostList(
        jwlToken: String,
        userId: Int
    ) {
        val res = DataRepository.askNewPost(
            jwlToken,
            userId,
            PostType.NO_TYPE.ordinal,
            latestPage.value + 1
        )
//        val res = postList
        res?.let {
            if(it.isNotEmpty()) {
                addLatestPostList(it)
                _latestPage.value++
            } else {
                _latestPage.value = -2 // 表示没有更多帖子可以获取
            }
        }
    }

    // 热门页面
    private val _popularPostList = MutableStateFlow<List<PostInfo>>(emptyList())
    val popularPostList: StateFlow<List<PostInfo>> = _popularPostList
    val popularListState = LazyListState()

    fun updatePopularPostList(posts: List<PostInfo>) {
        _popularPostList.value = posts
    }

    suspend fun refreshPopularPostList(
        jwlToken: String,
        userId: Int
    ) {
        val res = DataRepository.askHotPost(jwlToken, userId)
        res?.let {
            updatePopularPostList(it)
        }
    }

    // 收藏页面
    private val _favoritePostList = MutableStateFlow<List<PostInfo>>(emptyList())
    val favoritePostList: StateFlow<List<PostInfo>> = _favoritePostList
    private val _favoritePage = MutableStateFlow(0)
    val favoritePage: StateFlow<Int> = _favoritePage
    val favoriteListState = LazyListState()

    fun updateFavoritePostList(posts: List<PostInfo>) {
        _favoritePostList.value = posts
    }

    fun addFavoritePostList(posts: List<PostInfo>){
        _favoritePostList.update { current ->
            current + posts
        }
    }

    suspend fun refreshFavoritePostList(
        jwlToken: String,
        userId: Int
    ) {
        val res = DataRepository.getMyFavoritePosts(jwlToken, userId, 0)
        res?.let {
            updateFavoritePostList(it)
            _favoritePage.value = 0
        }
    }

    suspend fun getMoreFavoritePostList(
        jwlToken: String,
        userId: Int
    ){
        val res = DataRepository.getMyFavoritePosts(jwlToken,userId,favoritePage.value+1)
        res?.let{
            if(it.isNotEmpty()) {
                addFavoritePostList(it)
                _latestPage.value++
            } else {
                _latestPage.value = -2
            }
        }
    }

    // 搜索页面
    private val _searchPostList = MutableStateFlow<List<PostInfo>>(emptyList())
    val searchPostList: StateFlow<List<PostInfo>> = _searchPostList
    val searchListState = LazyListState()

    fun updateSearchPostList(posts: List<PostInfo>) {
        _searchPostList.value = posts
    }

    suspend fun searchPost(
        jwlToken: String,
        searchText: String,
        userId: Int
    ) {
        val res = DataRepository.searchPost(jwlToken, searchText, userId, PostType.NO_TYPE.ordinal)
        res?.let { updateSearchPostList(it) }
    }


}